public class Solution {
    public int minCost(int[][] costs) {
        int m = costs.length;

        int[][] dp = new int[m + 1][3];

        for (int i = 0; i < m; i++) {
            dp[i + 1][0] = Math.min(dp[i][1], dp[i][2]) + costs[i][0];
            dp[i + 1][1] = Math.min(dp[i][0], dp[i][2]) + costs[i][1];
            dp[i + 1][2] = Math.min(dp[i][1], dp[i][0]) + costs[i][2];
        }

        int ret = Integer.MAX_VALUE;

        for (int num : dp[m]) {
            ret = ret <= num ? ret : num;
        }

        return ret;
    }
}
